package com.zyl.ag.ui.ai;

import android.app.Activity;
import android.app.AlertDialog;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.zyl.ag.R;
import com.zyl.ag.SysApplication;
import com.zyl.ag.bean.AgTD;
import com.zyl.ag.bean.Record;
import com.zyl.ag.manager.RecordManager;
import com.zyl.ag.ui.widget.WidgetProvider;
import com.zyl.util.CalUtil;
import com.zyl.util.SharedPreferencesUtil;
import com.zyl.util.UrlUtil;

public class AiActivity  extends Activity {

	final Context context = this;

	private Button okButton;
	private RadioButton radioButton1,radioButton3;
	private EditText  editText2;
	private  TextView currentPrice,textView1,mTitleView,store;
 
	 RadioGroup group1,group2;
	 RecordManager recordManager;

	public void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(R.layout.ai_activity);
		 String str=UrlUtil.getAgTD();
		 AgTD ag=AgTD.getAgTD(str);  
		 recordManager =new RecordManager(context);
		
		mTitleView = (TextView) findViewById(R.id.title_text);
		currentPrice = (TextView) findViewById(R.id.currentPrice);
		textView1 = (TextView) findViewById(R.id.textView1);
		store = (TextView) findViewById(R.id.store);
		 
		editText2 = (EditText) findViewById(R.id.editText2);
	
		radioButton1= (RadioButton) findViewById(R.id.radioButton1);
		radioButton3= (RadioButton) findViewById(R.id.radioButton3);
	    group1 = (RadioGroup)this.findViewById(R.id.radioGroup1);
	    group2 = (RadioGroup)this.findViewById(R.id.radioGroup2);
	    okButton = (Button) findViewById(R.id.ok_Button);

	    mTitleView.setText("模拟交易");
	  //  currentPrice.setText(ag.getPrice() );
	    radioButton1.setChecked(true);
	    radioButton3.setChecked(true);
		currentPrice.setText(""+(int)Double.parseDouble( ag.getPrice() ));
		
		String stats=recordManager.stats("zyl");
		store.setText(stats);
	
		okButton.setOnClickListener(okClickListener);
	}
	 OnClickListener okClickListener=new OnClickListener() {
		public void onClick(View arg0) {
			 
			try {
				double buy=Double.parseDouble(currentPrice.getText().toString());
				int n=Integer.parseInt(editText2.getText().toString());
				Integer money=0;
				String name="zyl";
				 Integer price =(int)buy;
				 
				int type=1;
				int deal =1;
				if(group1.getCheckedRadioButtonId()==R.id.radioButton2) type=-1;
				if(group2.getCheckedRadioButtonId()==R.id.radioButton4) deal=-1;
				//double open=Double.parseDouble(editText3.getText().toString());买卖
				//double close=Double.parseDouble(editText4.getText().toString()); 
				money=(int)CalUtil.charge(buy );
				textView1.setText(money+"");
			
				
				Record	r =new Record(  name,  n,   price,
							  type,   deal,   money*n,   System.currentTimeMillis());
				recordManager.add(r);
				String stats=recordManager.stats("zyl");
				store.setText(stats);
			
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
			
			
		}
	};
	public void clear(View arg0) {
		 
	 
		textView1.setText( "");
		
		
	}
	
	public void refresh(View arg0) {
		String str = UrlUtil.getAgTD();
		AgTD ag = AgTD.getAgTD(str);
		currentPrice.setText(""+(int)Double.parseDouble( ag.getPrice() ));
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		 
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
		 
			 ShowExitdialog() ;
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	protected void ShowExitdialog() 
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(AiActivity.this);

		builder.setMessage("确定要退出吗?");

		builder.setTitle("提示");

		builder.setPositiveButton("确认",
				new android.content.DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
					 
			            
						dialog.dismiss();
						 SysApplication.getInstance().exit();
						//HomeActivity.this.finish();
					}

				});

		builder.setNegativeButton("取消",
				new android.content.DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

						dialog.dismiss();

					}

				});

		builder.create().show();

	}
}
